import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JPanel;

import edu.jhu.tcj.purfuit.Category;
import edu.jhu.tcj.purfuit.Game;
import edu.jhu.tcj.purfuit.Player;
import edu.jhu.tcj.purfuit.Turn;
import edu.jhu.tcj.purfuit.Turn.TurnState;


public class PlayerUI extends JPanel implements PropertyChangeListener{

	private int size = 100;
	
	private final Player player;
	private final Game game;

	public PlayerUI(Player player, Game game) {
		this.player = player;
		this.game = game;
		this.game.addPropertyChangeListener(Turn.TURN_STATE_UPDATE, this);
	}

	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		Graphics2D g = (Graphics2D)graphics;
		g.setColor(Color.black);
		g.fillRect(0, 0, size, size);
		g.setColor(Color.DARK_GRAY);
		g.fillRect(1, 1, size-2, size-2);
		Font nameFont = new Font("Tahoma", Font.BOLD, 16);
		g.setFont(nameFont);
		g.setColor(player.getColor());
		g.drawString(player.getName(), 10, 20);
		Category[] completedCategories = player.getCompletedCategories();
		
		int x = 10;
		int y = 22;
		int[] spacingX = {0,1,0,1};
		int[] spacingY = {0,0,1,1};
		
		for (int i = 0; i < completedCategories.length; i++) {
			Category category = completedCategories[i];
			g.setColor(category.getColor());
//			g.fillRect(x, y, (size-25)/2, (size-25)/2);
			
			graphics.fillRect(22 + 25*spacingX[i], 30 + 25*spacingY[i], 25, 25);
			graphics.setColor(Color.black);
			graphics.drawRect(22 + 25*spacingX[i], 30 + 25*spacingY[i], 25, 25 );
			
			
//			if (i == 0 || i == 2) {
//				x = x+(size-25)/2;
//				y = 22;
//			} else {
//				x = 10;
//				y = y+(size-25)/2;
//			}
			
		}
	}
	
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		Object newValue = evt.getNewValue();
		if (newValue instanceof TurnState) {
			TurnState turnState = (TurnState)newValue;
			switch (turnState) {
				case WaitingToRoll:
					break;
				case WaitingToChooseSpace:
					break;
				case WaitingToAnswerQuestion:
					break;
				case TurnOver:
					break;
			}
			repaint();
		}
	}
	
}
